﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Veteran_cms.Models;
namespace Veteran_cms.Controllers
{
    public class ManageContactController : BaseController
    {
        Veterandb ctx = new Veterandb();
        //
        // GET: /ManageContact/

        public ActionResult IndexContact()
        {
            var contact = (from contactus in ctx.contacts where contactus.Contact_id != 0 select contactus).SingleOrDefault();
            if (contact != null)
                return View(contact);
            else
            {
                contact = new contact();
                return View(contact);
            }   
        }
        [HttpPost]
        public ActionResult UpdateToContact(int id, FormCollection frm)
        {
            try
            {
                //if (!IsAuthenticateAdmin())
                //    return RedirectToAction("LogOn", "DashBoard");

                var contact = (from contactus in ctx.contacts where contactus.Contact_id == id select contactus).SingleOrDefault();
                if (contact != null)
                {
                    contact.Address = frm["address"];
                    contact.city = frm["city"];
                    contact.country = frm["country"];
                    contact.fax = frm["fax"];
                    contact.phone1 = frm["phone1"];
                    contact.mobile_phone = frm["mobile_phone"];

                    contact.title = frm["title"];
                    contact.direction = frm["direction"];
                    contact.write_to_us = frm["write_to_us"];
                    contact.content = frm["content"];
                   // contact.created_date = DateTime.Now;
                    UpdateModel(contact);
                    ctx.SaveChanges();
                    return RedirectToAction("IndexContact");
                }
                else
                {
                    contact = new contact();
                    contact.Address = frm["address"];
                    contact.city = frm["city"];
                    contact.country = frm["country"];
                    contact.fax = frm["fax"];
                    contact.phone1 = frm["phone1"];
                    contact.mobile_phone = frm["mobile_phone"];

                    contact.title = frm["title"];
                    contact.direction = frm["direction"];
                    contact.write_to_us = frm["write_to_us"];
                    contact.content = frm["content"];
                    contact.created_date = DateTime.Now;
                    ctx.AddTocontacts(contact);
                    ctx.SaveChanges();
                    return RedirectToAction("IndexContact");
                }
            }
            catch
            {
                return View();
            }
        }
    }
}
